import { ref, computed ,watch} from 'vue'
import { defineStore,storeToRefs } from 'pinia'
import { invokeAsyncAPi } from '@/utils/xz-fn'
import { useDownloadManageWindow } from '@/stores/download-manage-window'

// 配置数据
export const useUserInfo = defineStore('userInfo', () => {
  const isMember = ref(false);//是否是会员
  const isLogin = ref(false);//是否登录
  const headImg = ref('');//用户头像
  const nickName = ref('');//用户昵称
  const memberExpireTime = ref('');//用户过期时间
  const isNoLimitMember = ref(false);//是否是终身会员
  const token = ref('');//token
  
  const downloadManageWindowStore = useDownloadManageWindow();
  const { masterWindowId, downloadManageWindowId, downloadFinishWindowId } = storeToRefs(downloadManageWindowStore)

  const setIsLogin = (_isLogin:boolean) => {
    isLogin.value = _isLogin;
  }

  const setIsMember = (_isMember:boolean) => {
    isMember.value=_isMember
  }

  const setHeadImg=(_headImg:string)=>{
    headImg.value = _headImg;
  }

  const setNickName = (_name: string)=>{
    nickName.value = _name;
  }

  const setMemberExpireTime = (_time: string)=>{
    memberExpireTime.value = _time;
  }

  const setIsNoLimitMember = (_isNoLimit: boolean)=>{
    isNoLimitMember.value = _isNoLimit;
  }

  const setToken = (_token: string) => {
    token.value=_token
  }

  const sendIsMemberToManageWindow = () => {//下载完成小红点同步
    if (downloadManageWindowId.value) {//window存在
      invokeAsyncAPi('fq_NotifyWebMessageEx', downloadManageWindowId.value, 'download-manage-is-member',isMember.value);
    }
  }

  watch(isMember, (value) => {
    sendIsMemberToManageWindow();
  })

  return {
    isMember,
    isLogin,
    headImg,
    nickName,
    memberExpireTime,
    isNoLimitMember,
    token,
    setIsLogin,
    setIsMember,
    setHeadImg,
    setNickName,
    setMemberExpireTime,
    setIsNoLimitMember,
    setToken,
    sendIsMemberToManageWindow
  }
})
